package com.banzhi.shop.model.Impl;

import com.banzhi.shop.utils.ASUtils;
import com.google.gson.Gson;
import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.Management.Management;
import com.banzhi.shop.model.CallBack.ManagementCallBack;
import com.banzhi.shop.model.IManagementModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by HT on 2017/5/3.
 * 商品管理
 */

public class ManagementImpl implements IManagementModel {

    @Override
    public void getData(int page, final ManagementCallBack callBack) {
        getData(page, null, callBack);
    }

    @Override
    public void getData(ManagementCallBack callBack) {
        getData(0, callBack);
    }

    @Override
    public void getData(String keyword, ManagementCallBack callBack) {
        getData(0, keyword, callBack);
    }

    @Override
    public void getData(int page, String keyword, final ManagementCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.goodsManageUrl);
        if (ASUtils.isCanUse(keyword)) {
            params.addBodyParameter("keyword", keyword);
        }
        if (page != 0) {
            params.addBodyParameter("page", page + "");
        }
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("ManagementImpl11111111", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Management management = gson.fromJson(result, Management.class);
                        callBack.ongetDataSuccess(management.getRes());
                    } else {
                        callBack.ongetDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("ManagementImpl11111", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(6)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }


    /**
     * @param goods_id 这是商品id
     * @param brandid  这是品牌id
     *                 两个只能传一个
     * @param callBack 删除商品
     */
    @Override
    public void deleteGoods(String goods_id, String brandid, final ManagementCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.deleleShopUrl);
        if (goods_id != null) {
            params.addBodyParameter("goods_id", goods_id);
        }
        if (brandid != null) {
            params.addBodyParameter("brandid", brandid);
        }
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("ManagementImpl2222222", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        callBack.deleteGoodsSuccess(jsonobject);
                    } else {
                        callBack.deleteGoodsError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("ManagementImpl22222", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
